package com.xiaoge.testproxy;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * TODO
 *
 * @author <a href="mailto:1330137071@qq.com">Zhang Xiao</a>
 * @since
 */
//@Configuration
public class TestBeanNameAutoProxyCreator {

	@Bean
	public MethodInterceptor zhangXiaoAroundAdvice(){
		return new MethodInterceptor() {
			@Nullable
			@Override
			public Object invoke(@Nonnull MethodInvocation invocation) throws Throwable {
				System.out.println(".....before.....");
				Object result = invocation.proceed();  // target调用的方法
				System.out.println(".....after.....");
				return result;
			}
		};
	}

	@Bean
	public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
		BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
		// todo 根据bean名称做代理
		beanNameAutoProxyCreator.setBeanNames("userSe*");
		// todo 需要一个bean名称为zhangXiaoAroundAdvice的bean
		beanNameAutoProxyCreator.setInterceptorNames("zhangXiaoAroundAdvice");
		// todo 为false使用java动态代理, 为true使用cglib
		beanNameAutoProxyCreator.setProxyTargetClass(true);
		return beanNameAutoProxyCreator;
	}

}
